#include "mainboard.h"

MainBoard::MainBoard(QQuickItem *parent)
    : QQuickPaintedItem(parent)
{
    player_status = PAUSE;
//    player_process = NULL;
    volume_length = 100;

    player_timer = new QTimer();
    connect(&player_process, SIGNAL(readyReadStandardOutput()), this, SLOT(read_process_back_message()));
    connect(&player_process, SIGNAL(finished(int)), this, SIGNAL(process_finished(int)));
    connect(player_timer, SIGNAL(timeout()), this, SLOT(player_timer_out()));

    player_timer->start(500);
}

MainBoard::~MainBoard()
{
    player_timer->stop();
    delete player_timer;
}

void MainBoard::read_process_back_message()
{
    /* you have open player_process before step in this function */

    while (player_process.canReadLine()) {
        QByteArray buffer(player_process.readLine());
        qDebug(buffer);
        if (buffer.startsWith("ANS_LENGTH")) {
            buffer.remove(0, 11);
            process_length = QString(buffer).toFloat();
            qDebug("time length %f\n", process_length);
        } else if (buffer.startsWith("ANS_TIME_POSITION")) {
            buffer.remove(0,18);
            process_value = QString(buffer).toFloat();
            emit process_value_changed(process_value);
            qDebug("time length %f\n", process_length);
            qDebug("time pos %f\n", process_value);
//        }   else if (buffer.startsWith("ANS_PERCENT_POSITION")) {
//            buffer.remove(0, 21);
//            float percent = QString(buffer).toFloat();
//            process_value = process_length * percent / 100;
//            emit process_value_changed(process_value);
//            qDebug("time length %f\n", process_length);
//            qDebug("time pos %f\n", process_value);
        } else if (buffer.startsWith("ANS_volume")) {
            buffer.remove(0, 11);
            volume_length = 100;
            volume_value = QString(buffer).toFloat();
            qDebug("volume value %f\n", volume_value);
        }
    }
}

void MainBoard::resize_player_window()
{
}

void MainBoard::player_timer_out()
{
    if (player_status == PLAY) {
        if (process_length == 0) {
            player_process.write("get_time_length\n");
            player_process.write("get_property volume\n");
        }
        player_process.write("get_time_pos\n");
    }
}

void MainBoard::play_pause_btn_click_slots()
{
    if (!player_process.isOpen()) {
        player_status = READY;
    }

    if (player_status == STOP)
        return;

    player_status = (player_status == PAUSE || player_status == READY) ? PLAY : PAUSE;

    if (player_status == PLAY && !player_process.isOpen()) {
        QString cmd = QString("mplayer -slave -quiet -wid ") + QString::number(window()->winId()) + QString(" x11 -zoom ") + movie_file_path;
        qDebug(cmd.toLatin1());

        player_process.reset();
        player_process.start(cmd.toLatin1());
        if (!player_process.waitForStarted()) {
            qDebug("mplayer start failed\n");
        }

        /* we need to add output message about movie information for debug */
    } else {
        player_process.write("pause\n");
    }
}

void MainBoard::stop_btn_click_slots()
{
    player_status = STOP;

    if (player_process.isOpen()) {
        player_process.write("quit\n");
//        player_process.kill();
        if (!player_process.waitForFinished()) {
            qDebug("mplayer finish failed\n");
        }
        player_process.close();
        //        player_process.reset();
    }

    process_length = 0;
    process_value = 0;
    time_value = 0;
    time_length = 0;
}
void MainBoard::mute_btn_click_slots()
{
    mute_status = (++mute_status) % 2;

    QString cmd = QString("mute %1\n").arg(mute_status);
    player_process.write(cmd.toLatin1());
    if (mute_status == UNMUTE) {
        volume_slider_valuechanged_slots(volume_value);
    }
}

void MainBoard::volume_slider_valuechanged_slots(float value)
{
    volume_value = value * volume_length;

    if (player_process.isOpen() && mute_status == UNMUTE) {
        QString cmd = QString("volume %1 1\n").arg(volume_value);
        player_process.write(cmd.toLatin1());
    }
}

void MainBoard::process_slider_valuechanged_slots(float value)
{
//    process_value = value;
    float tmp_value = value * process_length;
//    time_value = percent * time_length;

    if (player_process.isOpen()) {
        QString cmd = QString("seek %1 2\n").arg(tmp_value);
        player_process.write(cmd.toLatin1());
    }
}

void MainBoard::close_btn_click_slots()
{
//    QApplication::quit();
}

void MainBoard::max_btn_click_slots()
{
    window_status = MAX_WINDOW;
//    this->showMaximized();
}

void MainBoard::min_btn_click_slots()
{
    window_status = MIN_WINDOW;
//    this->showMinimized();
}

void MainBoard::set_btn_click_slots()
{
    /* someting todo */
}

void MainBoard::paint(QPainter *painter)
{
}
